﻿package com.sz.courseserial.servlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import com.sz.courseserial.bmo.ICourseSerialBMO;

import org.springframework.web.servlet.mvc.Controller;

public class AddCourseSerial2 extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	protected final Log logger = LogFactory.getLog(getClass());
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		try {
			
			String rtn_type = request.getParameter("return_type");
			String path = request.getContextPath(); 
			
			ServletContext servletContext = request.getSession().getServletContext();        
			ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
			
			ICourseSerialBMO courseserialBMO = (ICourseSerialBMO) context.getBean("CourseSerialBMO");

			Map map = new HashMap();
			
			String name 		= request.getParameter("name");
			String descrip		= request.getParameter("descrip");
			String sj		= request.getParameter("sj");

			map.put("NAME", name);
			map.put("DESCRIP", descrip);
			map.put("DISPLAY_ORDER", "0");
			map.put("PARENT_ID", sj);
			map.put("SERIAL_LEVEL", "2");

			long r = courseserialBMO.addCourseSerial(map);
			logger.info(String.format("addCourseSerial返回:%d", r));			

			if(r > 0) {
				if(rtn_type != null && rtn_type.compareToIgnoreCase("message")==0){
					response.setCharacterEncoding("utf-8");
					response.getWriter().print("操作成功");
				} else {
					response.sendRedirect(path + "/page/success.jsp");
				}
			} else {
				if(rtn_type != null && rtn_type.compareToIgnoreCase("message")==0){
					response.setCharacterEncoding("utf-8");
					response.getWriter().print("操作失败");
				} else {
					response.sendRedirect(path + "/page/failure.jsp");

				}
			}
			
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new ServletException(e.getMessage());
		}
	}
}
